﻿namespace Blaze.Framework.LuaExtensions
{
    using UnityEngine;
    using XLua;

    /// <summary>
    /// 为<see cref="LuaBehaviour"/>提供功能扩展。
    /// </summary>
    public static class LuaBehaviourExtension
    {
        /// <summary>
        /// 为<see cref="GameObject"/>提供挂载指定lua脚本的<see cref="LuaBehaviour"/>组件的功能。
        /// </summary>
        /// <param name="obj">游戏对象</param>
        /// <param name="scriptPath">lua脚本路径</param>
        /// <returns></returns>
        public static LuaBehaviour AddLuaBehaviour(this GameObject obj, string scriptPath = null)
        {
            var behaviour = obj.AddComponent<LuaBehaviour>();
            behaviour.LuaFilePath = scriptPath;
            behaviour.Initialize();
            return behaviour;
        }

        /// <summary>
        /// 为<see cref="GameObject"/>提供挂载指定lua脚本的<see cref="LuaBehaviour"/>组件的功能。
        /// </summary>
        /// <param name="obj">游戏对象</param>
        /// <param name="scriptPath">lua脚本路径</param>
        /// <returns></returns>
        public static LuaTable AddLuaTable(this GameObject obj, string scriptPath = null)
        {
            var behaviour = obj.AddLuaBehaviour(scriptPath);
            return behaviour.LuaTable;
        }

        /// <summary>
        /// 为<see cref="GameObject"/>提供获取其上面挂载的<see cref="LuaBehaviour"/>组件的功能。
        /// </summary>
        /// <param name="obj">游戏对象</param>
        /// <param name="scriptPath">lua脚本路径</param>
        /// <returns></returns>
        public static LuaBehaviour GetLuaBehaviour(this GameObject obj, string scriptPath = null)
        {
            var luaBehaviours = obj.GetComponents<LuaBehaviour>();
            for (var i = 0; i < luaBehaviours.Length; i++)
            {
                var luaBehaviour = luaBehaviours[i];
                if (scriptPath == null || luaBehaviour.LuaFilePath == scriptPath)
                    return luaBehaviour;
            }

            return null;
        }

        /// <summary>
        /// 为<see cref="GameObject"/>提供获取其上面挂载的<see cref="LuaBehaviour"/>组件所对应的<see cref="LuaTable"/>的功能。
        /// </summary>
        /// <param name="obj">游戏对象</param>
        /// <param name="scriptPath">lua脚本路径</param>
        /// <returns></returns>
        public static LuaTable GetLuaTable(this GameObject obj, string scriptPath = null)
        {
            if (scriptPath == null)
            {
                var behaviour = obj.GetComponent<LuaBehaviour>();
                if (behaviour == null)
                    return null;
                if (behaviour.LuaTable == null)
                    behaviour.Initialize();
                return behaviour.LuaTable;
            }

            var luaBehaviours = obj.GetComponents<LuaBehaviour>();
            for (var i = 0; i < luaBehaviours.Length; i++)
            {
                var luaBehaviour = luaBehaviours[i];
                if (luaBehaviour.LuaFilePath == scriptPath)
                {
                    if (luaBehaviour.LuaTable == null)
                        luaBehaviour.Initialize();
                    return luaBehaviour.LuaTable;
                }
            }

            return null;
        }
    }
}